package com.jeesite.modules.tk.web;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.em.WeiquanStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.tk.entity.TkTaskWeiquan;
import com.jeesite.modules.tk.service.TkTaskWeiquanService;
import org.springframework.web.client.RestTemplate;

/**
 * 任务维权Controller
 * @author 李寒冰
 * @version 2025-02-08
 */
@Controller
@RequestMapping(value = "${adminPath}/tk/tkTaskWeiquan")
public class TkTaskWeiquanController extends BaseController {

	@Autowired
	private TkTaskWeiquanService tkTaskWeiquanService;

	//接口图片地址
	@Value("${pic.host}")
	private String picHost;


	//服务器地址
	@Value("${pic.baseUrl}")
	private String picBaseUrl;

	//接口地址
	@Value("${pic.reqUrl}")
	private String reqUrl;

	@Resource
	private RestTemplate restTemplate;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TkTaskWeiquan get(String id, boolean isNewRecord) {
		return tkTaskWeiquanService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("tk:tkTaskWeiquan:view")
	@RequestMapping(value = {"list", ""})
	public String list(TkTaskWeiquan tkTaskWeiquan, Model model) {
		model.addAttribute("tkTaskWeiquan", tkTaskWeiquan);
		return "modules/tk/tkTaskWeiquanList";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("tk:tkTaskWeiquan:view")
	@RequestMapping(value = {"caiding", ""})
	public String caiding(TkTaskWeiquan tkTaskWeiquan, Model model) {
		tkTaskWeiquan.setFailExplain(null);
		model.addAttribute("tkTaskWeiquan", tkTaskWeiquan);
		return "modules/tk/tkTaskWeiquanCaiding";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("tk:tkTaskWeiquan:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TkTaskWeiquan> listData(TkTaskWeiquan tkTaskWeiquan, HttpServletRequest request, HttpServletResponse response) {
		tkTaskWeiquan.setPage(new Page<>(request, response));
		if(WeiquanStatus.SHENSUZHONG.getCode().equals(tkTaskWeiquan.getShensuStatus())){
			tkTaskWeiquan.setShensuStatusNoeq(WeiquanStatus.JIEDAN.getCode());
			tkTaskWeiquan.setShensuStatus(null);
		}
		Page<TkTaskWeiquan> page = tkTaskWeiquanService.findPage(tkTaskWeiquan);
		List<TkTaskWeiquan> list = page.getList();
		for (TkTaskWeiquan tkTaskWeiquan1 : list) {
			String pics = tkTaskWeiquan1.getPics();
			JSONArray jsonArray = JSONArray.parseArray(pics);
			for (int i = 0; i < jsonArray.size(); i++) {
				String string = jsonArray.getString(i);
				if(StringUtils.isNotEmpty(string)&& !string.startsWith("http")){
					string = picHost+string;
				}
				jsonArray.set(i, string);
			}
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("tk:tkTaskWeiquan:view")
	@RequestMapping(value = "form")
	public String form(TkTaskWeiquan tkTaskWeiquan, Model model) {
		model.addAttribute("tkTaskWeiquan", tkTaskWeiquan);
		return "modules/tk/tkTaskWeiquanForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("tk:tkTaskWeiquan:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TkTaskWeiquan tkTaskWeiquan) {
		String pics = tkTaskWeiquan.getPics();
		String[] split = pics.split(",");
		JSONArray jsonArray = new JSONArray();
		for (int i = 0; i < split.length; i++) {
			String pic = split[i];
			jsonArray.add(picBaseUrl+pic);
		}
		tkTaskWeiquan.setPics(jsonArray.toJSONString());
		JSONObject jsonObjectWeiquan = new JSONObject();
		jsonObjectWeiquan.put("id",tkTaskWeiquan.getId());
		jsonObjectWeiquan.put("pics",tkTaskWeiquan.getPics());
		jsonObjectWeiquan.put("shensuStatus",tkTaskWeiquan.getShensuStatus());
		jsonObjectWeiquan.put("failExplain",tkTaskWeiquan.getFailExplain());
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		HttpEntity<String> jsonEntity = new HttpEntity<String>(jsonObjectWeiquan.toJSONString(), headers);
		String s = restTemplate.postForObject(reqUrl + "/api/task/saveShensu", jsonEntity, String.class);
		JSONObject jsonObject = JSONObject.parseObject(s);
		String code = jsonObject.getString("code");
		if("200".equals(code)){
			return renderResult(Global.TRUE, text("裁定成功！"));
		}else{
			return renderResult(Global.FALSE, text("裁定失败！"));
		}
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("tk:tkTaskWeiquan:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TkTaskWeiquan tkTaskWeiquan) {
		tkTaskWeiquanService.delete(tkTaskWeiquan);
		return renderResult(Global.TRUE, text("删除任务维权成功！"));
	}
	
}